package com.gupt.weekly.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gupt.weekly.domain.User;
import com.gupt.weekly.domain.UserRole;
import com.gupt.weekly.dto.UserInfo;
import com.gupt.weekly.service.UserRoleService;
import com.gupt.weekly.service.UserService;
import com.gupt.weekly.vo.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author linzizun
 * @version 1.0
 * @date 2021/03/09 16:22
 */
@Slf4j
@RestController
@Api(tags = "用户模块接口")
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @Resource
    private UserRoleService userRoleService;

    private static final BeanCopier USER_INFO_2_USER =
            BeanCopier.create(UserInfo.class, User.class, false);

    private static final BeanCopier USER_2_USER_INFO =
            BeanCopier.create(User.class, UserInfo.class, false);

    @ApiOperation(value = "创建用户", notes = "创建新用户")
    @PostMapping(value = "/createUser")
    public CommonResult createUser(@Valid @RequestBody User user) {
        user.setId(null);
        int result = userService.insert(user);
        if (result > 0) {
            return new CommonResult(200, "插入数据库成功", result);
        } else {
            return new CommonResult(444, "插入数据库失败");
        }
    }

    @ApiOperation(value = "查询用户名", notes = "查询账号是否存在")
    @GetMapping(value = "/hasUsername")
    public CommonResult hasUsername(String username) {
        int result = userService.hasUsername(username);
        if (result > 0) {
            return new CommonResult(200, "用户名存在", result);
        } else {
            return new CommonResult(200, "用户名不存在", result);
        }
    }

    @ApiOperation(value = "登录", notes = "账号密码登录")
    @PostMapping(value = "/login")
    public CommonResult login(@RequestParam String username, @RequestParam String password) {
        System.out.println(username + password);
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user = userService.selectByUsernameAndPassword(user);
        if (null != user) {
            return new CommonResult(200, "登录成功", user.getId());
        } else {
            return new CommonResult(444, "登录失败");
        }
    }

    @ApiOperation(value = "获取用户信息", notes = "通过id获取用户信息")
    @GetMapping(value = "/getUserInfoById")
    public CommonResult getUserInfoById(String id) {
        UserInfo userInfo = new UserInfo();
        USER_2_USER_INFO.copy(userService.selectById(id), userInfo, null);
        if (null != userInfo) {
            return new CommonResult(200, "查询成功", userInfo);
        } else {
            return new CommonResult(444, "id不存在");
        }
    }

    @ApiOperation(value = "模糊查询用户信息", notes = "通过用户名、姓名或手机获取用户信息列表")
    @GetMapping(value = "/getUserInfoByCondition")
    public CommonResult getUserInfoByCondition(String condition) {
        List<UserInfo> userInfoList = new ArrayList<>();
        List<User> userList = userService.selectByCondition(condition);
        for (User user : userList) {
            UserInfo userInfo = new UserInfo();
            USER_2_USER_INFO.copy(user, userInfo, null);
            userInfoList.add(userInfo);
        }
        if (userInfoList.size() > 0) {
            return new CommonResult(200, "查询成功", userInfoList);
        } else {
            return new CommonResult(200, "无相关用户");
        }
    }

    @ApiOperation(value = "更新用户信息", notes = "更新用户信息")
    @PostMapping(value = "/updateUserInfo")
    public CommonResult updateUserInfo(@Valid @RequestBody UserInfo userInfo) {
        userInfo.setUsername(null);
        User user = new User();
        USER_INFO_2_USER.copy(userInfo, user, null);
        int result = userService.updateInfoById(user);
        if (result > 0) {
            return new CommonResult(200, "更新数据成功", result);
        } else {
            return new CommonResult(444, "更新数据失败");
        }
    }

    @ApiOperation(value = "更改密码", notes = "更改用户密码")
    @PostMapping(value = "/updatePassword")
    public CommonResult updatePassword(@RequestParam String username,
                                       @RequestParam String old_password,
                                       @RequestParam String new_password) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(old_password);
        int result = userService.updatePasswordById(user, new_password);
        if (result > 0) {
            return new CommonResult(200, "更新数据成功", result);
        } else {
            return new CommonResult(444, "更新数据失败");
        }
    }

    @ApiOperation(value = "关联角色", notes = "用户关联角色")
    @PostMapping(value = "/creatUserRole")
    public CommonResult creatUserRole(@Valid @RequestBody UserRole userRole) {
        userRole.setId(null);
        boolean result = userRoleService.save(userRole);
        if (result) {
            return new CommonResult(200, "插入数据库成功");
        } else {
            return new CommonResult(444, "插入数据库失败");
        }
    }

    @ApiOperation(value = "获取用户角色列表", notes = "获取全部用户角色列表")
    @GetMapping(value = "/getUserRoleList")
    public CommonResult getUserRoleList() {
        List<UserRole> userRoleList = userRoleService.list();
        if (userRoleList.size() > 0) {
            return new CommonResult(200, "查询数据库成功", userRoleList);
        } else {
            return new CommonResult(444, "查询数据库失败");
        }
    }

    @ApiOperation(value = "id查询用户角色", notes = "根据id查询某用户关联的角色")
    @GetMapping(value = "/selectUserRoleById")
    public CommonResult selectUserRoleById(String id) {
        UserRole userRole = userRoleService.getById(id);
        if (null != userRole) {
            return new CommonResult(200, "查询数据库成功", userRole);
        } else {
            return new CommonResult(444, "查询数据库失败");
        }
    }

    @ApiOperation(value = "查询用户角色", notes = "条件查询用户角色")
    @PostMapping(value = "/selectUserRole")
    public CommonResult selectUserRole(@RequestBody UserRole userRole) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", userRole.getId())
                .or().eq("user_id", userRole.getUser_id())
                .or().eq("role_id", userRole.getRole_id());
        List<UserRole> userRoleList = userRoleService.list(queryWrapper);
        if (userRoleList.size() > 0) {
            return new CommonResult(200, "查询数据库成功", userRoleList);
        } else {
            return new CommonResult(444, "查询数据库失败");
        }
    }

    @ApiOperation(value = "用户名查询用户角色", notes = "根据用户名查询某用户关联的角色")
    @GetMapping(value = "/selectUserRoleByUserId")
    public CommonResult selectUserRoleByUserId(String user_id) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", user_id);
        List<UserRole> userRoleList = userRoleService.list(queryWrapper);
        if (userRoleList.size() > 0) {
            return new CommonResult(200, "查询数据库成功", userRoleList);
        } else {
            return new CommonResult(444, "查询数据库失败");
        }
    }

    @ApiOperation(value = "角色id查询用户角色", notes = "根据角色id查询某用户关联的角色")
    @GetMapping(value = "/selectUserRoleByRoleId")
    public CommonResult selectUserRoleByRoleId(String role_id) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", role_id);
        List<UserRole> userRoleList = userRoleService.list(queryWrapper);
        if (userRoleList.size() > 0) {
            return new CommonResult(200, "查询数据库成功", userRoleList);
        } else {
            return new CommonResult(444, "查询数据库失败");
        }
    }

    @ApiOperation(value = "更改用户角色", notes = "更改用户角色")
    @PostMapping(value = "/updateUserRole")
    public CommonResult updateUserRole(@Valid @RequestBody UserRole userRole) {
        boolean result = userRoleService.updateById(userRole);
        if (result) {
            return new CommonResult(200, "更新数据成功");
        } else {
            return new CommonResult(444, "更新数据失败");
        }
    }
}
